package com.biz.primus.ms.stock.dao;

import com.biz.primus.model.stock.vo.StockChangeLogListReqVo;
import com.biz.primus.ms.stock.dao.po.StockChangeLog;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * ChangeLogSepcification
 *
 * @author JKLiues
 * @date 2017年11月03日
 */
@AllArgsConstructor
public class ChangeLogSpecification implements Specification<StockChangeLog> {

    private StockChangeLogListReqVo reqVo;

    @Override
    public Predicate toPredicate(Root<StockChangeLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(reqVo.getDepotCode())) {
            predicates.add(cb.equal(root.get("depotCode").as(String.class), reqVo.getDepotCode()));
        }
        if (StringUtils.isNotBlank(reqVo.getProductCode())) {
            predicates.add(cb.equal(root.get("productCode").as(String.class), reqVo.getProductCode()));
        }
        if (StringUtils.isNotBlank(reqVo.getBn())) {
            predicates.add(cb.equal(root.get("bn").as(String.class), reqVo.getBn()));
        }
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    }
}